<template>
    <div id="edit_paramter" class="Row_content" style="width:450px;">
      <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110">
        <FormItem label="参数名称：" prop="paramterName">
          <Input v-model="formValidate.paramterName" placeholder="请输入" class='input-wid' :maxlength='10' @on-keyup="filterName('paramterName')" @on-blur="filterName('paramterName')"></Input>
        </FormItem>
        <FormItem label="属性名称：" prop="classifyName">
          <Select v-model="formValidate.classifyName" class='input-wid' :disabled="flag?true:false">
            <Option v-for="(item, index) in classifyNameList" :value="item.packageId" :key="index">{{item.packageName}}</Option>
          </Select>
        </FormItem>
        <FormItem label="是否必填信息：" prop="msgShow">
          <RadioGroup v-model="formValidate.msgShow" size="large">
            <Radio label="1">是</Radio>
            <Radio label="0" style="margin-left:40px">否</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="是否显示：" prop="isShow">
          <RadioGroup v-model="formValidate.isShow" size="large">
            <Radio label="1">显示</Radio>
            <Radio label="0" style="margin-left:40px">隐藏</Radio>
          </RadioGroup>
        </FormItem>
        <FormItem label="参数录入方式：" prop="selectWay">
          <RadioGroup v-model="formValidate.selectWay" size="large" @on-change='onChange'>
            <Radio label="0" :disabled="flag?true:false">手动</Radio>
            <Radio label="1" style="margin-left:25px" :disabled="flag?true:false">单选</Radio>
            <Radio label="2" style="margin-left:25px" :disabled="flag?true:false">多选</Radio>
          </RadioGroup>
        </FormItem>
        <!-- <FormItem label="参数列表：" prop="selectParamter" v-if="formValidate.selectWay === '1' || formValidate.selectWay === '2'">
        <Input v-model="formValidate.selectParamter" type="textarea" :maxlength='300' :autosize="{ minRows: 4, maxRows: 8 }" class="Row_content_input" style='width:320px' @on-change='changeList' :disabled="flag?true:false"></Input>
          <p style="color: #808695;">一行代表一个可选值</p>
        </FormItem> -->
        <FormItem label="参数列表：" prop="paramList" v-if="formValidate.selectWay === '1' || formValidate.selectWay === '2'">
          <div class="select-param">
            <Input v-model="selectParamter" style="border: 0" placeholder="请输入需要录入的参数选项" :maxlength='10' :disabled="(formValidate.paramList.length > 9 || flag) ? true: false">
            </Input>
            <Button @click='submitParam' style='background-color: #f8f8f9;border: 0' :disabled="(formValidate.paramList.length > 9 || flag) ? true: false">提交</Button>
          </div>
          <Table border :columns="columns" :data="formValidate.paramList" style="margin-top: 15px;width: 330px;" class="table-param">
            <template slot-scope="{ row,index }" slot="action">
              <i class="iconsys icondel" title="删除" @click="delParam(row, index)"></i>
            </template>
          </Table>
        </FormItem>
        <FormItem style="margin-top: 36px">
          <Button type="primary" class="btn" @click="handleSubmit">确定</Button>
          <Button class="btn" style="margin-left: 24px" @click="handleReset">取消</Button>
        </FormItem>
      </Form>
    </div>
  </template>

<script>
import { getAttributeList, editParams, detailParams } from '@/api/commondity/index'
import { routeEqual } from '@/libs/util'
export default {
  name: 'edit_paramter',
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (vm.propNameId !== to.params.id) {
        vm.propNameId = to.params.id
        vm.paramsId = to.query.id
        vm.getDetail()
      }
    })
  },
  data () {
    return {
      columns: [
        {
          title: '录入参数值',
          key: 'value'
        },
        {
          title: '操作',
          slot: 'action',
          width: 125
        }
      ],
      selectParamter: '',
      paramsId: '',
      submitLock: false,
      propNameId: '',
      classifyNameList: [],
      flag: false,
      formValidate: {
        paramterName: '',
        classifyName: '',
        msgShow: '',
        isShow: '',
        selectWay: '',
        paramList: []
      },
      ruleValidate: {
        paramterName: { required: true, message: '请填写参数名称', trigger: 'change' },
        classifyName: { required: true, message: '请填写属性名称', trigger: 'change' },
        msgShow: { required: true, message: '请选择是否必填信息', trigger: 'change' },
        isShow: { required: true, message: '请选择是否显示', trigger: 'change' },
        selectWay: { required: true, message: '请选择参数录入方式', trigger: 'change' },
        paramList: {
          required: true,
          type: 'array',
          // trigger: 'blur',
          min: 1,
          message: '请录入参数选项'
        }
      }
    }
  },
  methods: {
    filterName (params) {
      this.formValidate[params] = this.formValidate[params].replace(/[^\u4e00-\u9fa5]/g, '')
    },
    delParam (row, index) {
      if (this.flag) {
        this.$Message.error('该参数已存在使用商品，无法进行编辑操作')
      } else {
        this.formValidate.paramList.splice(index, 1)
      }
    },
    submitParam () {
      if (this.selectParamter) {
        this.formValidate.paramList.push({ value: this.selectParamter })
        this.selectParamter = ''
      } else {
        this.$Message.error('请录入内容后提交')
      }
    },
    handleSubmit () {
      this.$refs.formValidate.validate(valid => {
        if (valid) {
          let data = {
            merchantId: this.$store.state.user.merchantId,
            packageId: this.formValidate.classifyName,
            propName: this.formValidate.paramterName,
            inputType: this.formValidate.selectWay,
            notNull: this.formValidate.msgShow,
            searchStatus: this.formValidate.isShow,
            updater: this.$store.state.user.userId,
            updaterName: this.$store.state.user.userName,
            propNameId: this.propNameId
          }
          if (this.formValidate.selectWay === '1' || this.formValidate.selectWay === '2') {
            let arr = []
            this.formValidate.paramList.forEach(item => {
              arr.push(item.value)
            })
            data.propValues = arr
            // let arr = []
            // this.formValidate.selectParamter.split('\n').forEach(item => {
            //   if (item.trim()) {
            //     arr.push(`${item.trim()}`)
            //   }
            // })
            // if (arr.length < 11) {
            //   data.propValues = arr
            // } else {
            //   this.$Message.error('最多可录入10个参数')
            //   return
            // }
          }
          if (this.submitLock) {
            return
          }
          this.submitLock = true
          editParams(data).then(res => {
            this.submitLock = false
            const result = res.data || {}
            if (result.status === 200) {
              this.$Message.success('编辑成功!')
              this.handleReset()
            } else {
              this.$Message.error(`${result.error}`)
            }
          }).catch(err => {
            this.submitLock = false
            this.$Message.error(`${err}`)
          })
        }
      })
    },
    handleReset () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'paramter_list',
          params: { id: this.formValidate.classifyName }
        })
      })
    },
    onChange () {},
    changeList (event) {
      this.formValidate.selectParamter = this.formValidate.selectParamter.replace(/ /g, '')
    },
    // 获取属性列表
    getList () {
      getAttributeList({
        merchantId: this.$store.state.user.merchantId
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.classifyNameList = result.data
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    },
    getDetail () {
      detailParams({
        merchantId: this.$store.state.user.merchantId,
        propNameId: this.propNameId
      }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          this.flag = result.data.flag
          this.formValidate = {
            paramterName: result.data.propName,
            classifyName: result.data.packageId,
            msgShow: result.data.notNull + '',
            isShow: result.data.searchStatus + '',
            selectWay: result.data.inputType + '',
            paramList: []
          }
          if (result.data.propertyValueList.length > 0) {
            result.data.propertyValueList.forEach(item => {
              this.formValidate.paramList.push({ value: item.propValue })
            })
          }
          // if (this.formValidate.selectParamter) {
          //   this.formValidate.selectParamter = this.formValidate.selectParamter.split(',').map(item => {
          //     return item
          //   }).join('\n')
          // }
        } else {
          this.$Message.error(`${result.error}`)
        }
      })
    }
  },
  mounted () {
    this.propNameId = this.$route.params.id
    this.paramsId = this.$route.query.id
    this.getList()
    this.getDetail()
  }
}
</script>

<style lang="less">
#edit_paramter{
  .input-wid{ width: 260px;}
.btn{ width: 100px;}
.select-param{
    display: flex;
    border: 1px solid #dcdee2;
    border-radius: 4px;
    width: 260px;
    overflow: hidden;
    .ivu-input{
      border: 0 !important;
      border-radius: 0 !important;
      /* border-top-left-radius: 4px;
      border-bottom-left-radius: 4px; */
    }
    .ivu-btn{
      border-radius: 0 !important;
      border-left: 1px solid #dcdee2 !important;
    }
  }
  .table-param{
    .ivu-table th, .ivu-table td{
      height: 37px !important;
    }
  }
}
</style>
